/*
 * Copyright 2009 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Date implements Comparable<Date> {

	private int value;

	private transient Calendar calendar;

	public Date(String date) {

		this.value = toInteger(date);
	}

	public Date(int day, int month, int year) {

		value = day;
		value |= (month << 6);
		value |= (year << 10);
	}

	@SuppressWarnings("unused")
	private Date() {
	}

	public static Date january(int day, int year) {
		return new Date(day, 1, year);
	}

	public static Date february(int day, int year) {
		return new Date(day, 2, year);
	}

	public static Date march(int day, int year) {
		return new Date(day, 3, year);
	}

	public static Date april(int day, int year) {
		return new Date(day, 4, year);
	}

	public static Date may(int day, int year) {
		return new Date(day, 5, year);
	}

	public static Date june(int day, int year) {
		return new Date(day, 6, year);
	}

	public static Date july(int day, int year) {
		return new Date(day, 7, year);
	}

	public static Date august(int day, int year) {
		return new Date(day, 8, year);
	}

	public static Date setember(int day, int year) {
		return new Date(day, 9, year);
	}

	public static Date october(int day, int year) {
		return new Date(day, 10, year);
	}

	public static Date november(int day, int year) {
		return new Date(day, 11, year);
	}

	public static Date december(int day, int year) {
		return new Date(day, 12, year);
	}

	int getYear() {
		return value >> 10;
	}

	int getMonth() {
		return (value >> 6) & 0xf;
	}

	int getDay() {
		return value & 0x3f;
	}

	public static int toInteger(String date) {

		int result;

		int index1 = date.indexOf('-');
		result = Integer.parseInt(date.substring(0, index1));

		int index2 = date.lastIndexOf('-');
		result += monthToInteger(date.substring(index1 + 1, index2)) * 100;

		String year = date.substring(index2 + 1);
		result += (year.length() > 2 ? Integer.parseInt(year) : 2000 + Integer
				.parseInt(year)) * 10000;

		return result;
	}

	private static int monthToInteger(String month) {

		if ("Apr".equals(month))
			return 4;

		throw new IllegalArgumentException("Unknown month: " + month);
	}

	public int compareTo(Date date) {
		return value - date.value;
	}

	@Override
	public java.lang.String toString() {

		return toString(Language.ENGLISH);
	}

	public java.lang.String toString(Language language) {

		if (calendar == null) {
			calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, getYear());
			calendar.set(Calendar.MONTH, getMonth());
			calendar.set(Calendar.DATE, getDay());
		}

		return SimpleDateFormat.getDateInstance(DateFormat.MEDIUM,
				language.getLocale()).format(calendar.getTime());
	}
}
